package com.fixme4trade.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
public class Product implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	//@GeneratedValue
	@SequenceGenerator( name = "PRODUCT_ID", sequenceName = "PRODUCT_SEQ", allocationSize = 1 )  
	@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "PRODUCT_ID" )
	@Column(name="product_id")
	private Long id;

	@OneToMany(mappedBy="product")
	private List<Sell> sellList = new ArrayList<Sell>();
	
	@ElementCollection
	@CollectionTable(
	        name="product_image",
	        joinColumns=@JoinColumn(name="product_id")
	)
	private List<Image> imageList = new ArrayList<Image>();

	@NotNull
	//@NotEmpty	
	@Size(max = 35)
	private String itemName;
	
	@NotNull
	//@NotEmpty	
	@Size(max = 150)
	private String description;

	@NotNull	
	private double price;

	@NotNull	
	private int quantity;

	@OneToOne(cascade = CascadeType.REFRESH, optional=true, fetch=FetchType.EAGER, orphanRemoval=false)
	@JoinColumn(name="product_rule_id", nullable=true)
	private ProductRule productRule;
	
	@OneToOne(cascade = CascadeType.REFRESH, optional=true, fetch=FetchType.EAGER, orphanRemoval=false)
	@JoinColumn(name="message_list_id", referencedColumnName="message_list_id", nullable=true)
	private MessageList messageAboutProduct;

	@ManyToOne
	@JoinColumn(name="seller_id", referencedColumnName="seller_id", nullable=false)
	private Seller owner;

	@OneToOne(cascade = CascadeType.REFRESH, optional=true, fetch=FetchType.EAGER, orphanRemoval=false)
	@JoinColumn(name="forum_id", referencedColumnName="message_list_id", nullable=true)
	private Forum productForum;
	
	public Product() {

	}

	public Long getId() {
		return id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(final String description) {
		this.description = description;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Seller getOwner() {
		return owner;
	}

	public void setOwner(Seller owner) {
		this.owner = owner;
	}

	public List<Image> getImageList() {
		return imageList;
	}

	public void setImageList(List<Image> imageList) {
		this.imageList = imageList;
	}
	
	public Forum getProductForum() {
		return productForum;
	}

	public void setProductForum(Forum productForum) {
		this.productForum = productForum;
	}

	public ProductRule getProductRule() {
		return productRule;
	}

	public void setProductRule(ProductRule productRule) {
		this.productRule = productRule;
	}

	public List<Sell> getSellList() {
		return sellList;
	}

	public void setSellList(List<Sell> sellList) {
		this.sellList = sellList;
	}
	
	public MessageList getMessageAboutProduct() {
		return messageAboutProduct;
	}

	public void setMessageAboutProduct(MessageList messageAboutProduct) {
		this.messageAboutProduct = messageAboutProduct;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}
}
